﻿using System.Collections.Concurrent;
using ComponentSystem;
using Cysharp.Threading.Tasks;
using DataCenterSystem.EasySave3;
using UnityEngine;

namespace DataCenterSystem
{
    /// <summary>
    /// 数据服务实体
    /// </summary>
    public partial class DataService_Entity : EntityCSharp
    {
        /// <summary>
        /// 单例实体
        /// </summary>
        public static DataService_Entity Instance = null;
        
        /// <summary>
        /// 文件存储组件
        /// </summary>
        protected FileSave_Com fileSave => this.GetComponent<FileSave_Com>();
        /// <summary>
        /// Prefs缓存组件
        /// </summary>
        protected PrefsCache_Com prefsSave => this.GetComponent<PrefsCache_Com>();
        /// <summary>
        /// 内存缓存组件
        /// </summary>
        protected MemoryCache_Com memoryCache => this.GetComponent<MemoryCache_Com>();
        
        /// <summary>
        /// 自定义修改 增加了自定义存储路径 CustomPath
        /// </summary>
        public enum Directory{ PersistentDataPath, DataPath, CustomPath }
    }
    
    public partial class DataService_Entity
    {
        /// <summary>
        /// 初始化
        /// </summary>
        protected override void Awake()
        {
            if(Instance != null) return;
            Instance = this;
            base.Awake();
            UniTask.ToCoroutine(async () =>
            {
                this.AddComponent<FileSave_Com>();
                this.AddComponent<PrefsCache_Com>();
                this.AddComponent<MemoryCache_Com>();
            });
        }

        #region Settings

        /// <summary>
        /// 设置所有存储路径
        /// </summary>
        public virtual async UniTask Setting_SavePath_All(Directory pathEnum, string path = "")
        {
            await Setting_SavePath_File(pathEnum, path);
            await Setting_SavePath_Prefs(pathEnum, path);
            await Setting_SavePath_Memory(pathEnum, path);
        }

        /// <summary>
        /// 设置文件存储路径
        /// </summary>
        public virtual async UniTask Setting_SavePath_File(Directory pathEnum, string path = "")
        {
            await fileSave.Setting_SavePath((ES3.Directory) (int)pathEnum, path);
        }

        /// <summary>
        /// 设置Prefs缓存路径
        /// </summary>
        public virtual async UniTask Setting_SavePath_Prefs(Directory pathEnum, string path = "")
        {
            await prefsSave.Setting_SavePath((ES3.Directory) (int)pathEnum, path);
        }

        /// <summary>
        /// 设置内存缓存路径
        /// </summary>
        public virtual async UniTask Setting_SavePath_Memory(Directory pathEnum, string path = "")
        {
            await memoryCache.Setting_SavePath((ES3.Directory) (int)pathEnum, path);
        }
        
        #endregion
        
        #region File

        /// <summary>
        /// 保存或更新文件
        /// </summary>
        public virtual async UniTask SaveOrUpdate_File<T>(string key, T data)
        {
            await fileSave.AddOrUpdateData(key, data);
        }

        /// <summary>
        /// 加载文件
        /// </summary>
        public virtual async UniTask<T> LoadData_File<T>(string key)
        {
            return await fileSave.LoadData<T>(key);
        }

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        public virtual async UniTask<bool> ContainsKey_File(string key)
        {
            return await fileSave.ContainsKey(key);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        public virtual async UniTask<bool> DeletData_File(string key)
        {
            return await fileSave.DeletData(key);
        }

        /// <summary>
        /// 清除文件
        /// </summary>
        public virtual async UniTask<bool> ClearSave_File()
        {
            return await fileSave.ClearSave();
        }

        #endregion

        #region PrefsCache_Com

        /// <summary>
        /// 保存或更新Prefs缓存
        /// </summary>
        public virtual async UniTask SaveOrUpdate_Prefs<T>(string key, T data)
        {
            await prefsSave.AddOrUpdateData(key, data);
        }

        /// <summary>
        /// 加载Prefs缓存
        /// </summary>
        public virtual async UniTask<T> LoadData_Prefs<T>(string key)
        {
            return await prefsSave.LoadData<T>(key);
        }

        /// <summary>
        /// 检查Prefs缓存是否存在
        /// </summary>
        public virtual async UniTask<bool> ContainsKey_Prefs(string key)
        {
            return await prefsSave.ContainsKey(key);
        }

        /// <summary>
        /// 删除Prefs缓存
        /// </summary>
        public virtual async UniTask<bool> DeletData_Prefs(string key)
        {
            return await prefsSave.DeletData(key);
        }

        /// <summary>
        /// 清除Prefs缓存
        /// </summary>
        public virtual async UniTask<bool> ClearSave_Prefs()
        {
            return await prefsSave.ClearSave();
        }

        #endregion

        #region MemoryCache_Com

        /// <summary>
        /// 保存或更新内存缓存
        /// </summary>
        public virtual async UniTask SaveOrUpdate_Memory<T>(string key, T data, bool autoClear = false)
        {
            await memoryCache.AddOrUpdateCache(key, data, autoClear);
        }

        /// <summary>
        /// 加载内存缓存
        /// </summary>
        public virtual async UniTask<T> LoadData_Memory<T>(string key)
        {
            return await memoryCache.LoadData<T>(key);
        }

        /// <summary>
        /// 检查内存缓存是否存在
        /// </summary>
        public virtual async UniTask<bool> ContainsKey_Memory(string key)
        {
            return await memoryCache.ContainsKey(key);
        }

        /// <summary>
        /// 删除内存缓存
        /// </summary>
        public virtual async UniTask<bool> DeletData_Memory(string key)
        {
            return await memoryCache.DeletData(key);
        }

        /// <summary>
        /// 清除内存缓存
        /// </summary>
        public virtual async UniTask<bool> ClearSave_Memory()
        {
            return await memoryCache.ClearSave();
        }

        #endregion
    }
}